package whf.framework.web.tag;

import java.util.Collection;

import javax.servlet.jsp.JspException;

import whf.framework.entity.Entity;
import whf.framework.log.Log;
import whf.framework.log.LogFactory;
import whf.framework.meta.MetaFactory;
import whf.framework.service.QueryService;
import whf.framework.util.BeanFactory;
import whf.framework.util.StringUtils;
import whf.framework.util.Utils;
import whf.framework.web.tag.ext.TagConstants;

/**
 * @author wanghaifeng
 *
 */
public class HtmlSelectTag extends HtmlTag{
	private static Log log = LogFactory.getLog(HtmlSelectTag.class);
	//use a datasource from some attribute
	private String dataSourceName;
	
	//use a exists datasource
	private Collection<Entity> dataSource;
	
	//use a bo class
	private String boClassName;
	
	//use a bo query
	private String query;
	
	//select condition
	private String selectedProperty;
	
	private Object selectedValues;
	
	public void setDataSource(Collection<Entity> dataSource) {
		this.dataSource = dataSource;
	}

	public void setDataSourceName(String dataSourceName) {
		this.dataSourceName = dataSourceName;
	}

	public void setSelectedValues(Object selectedValue) {
		this.selectedValues = selectedValue;
	}

	public void setBoClassName(String boClassName) {
		this.boClassName = boClassName;
	}

	public void setQuery(String query) {
		this.query = query;
	}

	public void setSelectedProperty(String selectedProperty) {
		this.selectedProperty = selectedProperty;
	}

	private void initParameters() throws JspException{
		if(this.dataSource == null && !StringUtils.isEmpty(this.dataSourceName)){
			Object dsObj = null;
			dsObj = pageContext.getAttribute(this.dataSourceName);
			if(dsObj == null){
				dsObj = request().getAttribute(this.dataSourceName);
			}
			if(dsObj == null){
				dsObj = pageContext.getSession().getAttribute(this.dataSourceName);
			}
			if(dsObj != null && (dsObj instanceof Collection)){
				this.dataSource = (Collection)dsObj;
			}
		}
		if(this.dataSource == null && !StringUtils.isEmpty(this.boClassName)){
			try{
				QueryService service = BeanFactory.getService(MetaFactory.findByModelClass(Utils.getClassByClassName(this.boClassName)));
				this.dataSource = service.query(this.query);
			}catch(Exception e){
				log.debug(this, e);
			}
		}
		
		String size = super.getAttribute(TagConstants.ATTRIBUTE_SIZE);
		if(StringUtils.equalsIgnoreCase(size, "auto") && this.dataSource != null){
			size = String.valueOf(this.dataSource.size());
		}
	}
	
	public int doStartTag() throws JspException{
		this.initParameters();
		String[] ids = null;
		if(this.selectedValues instanceof String){
			ids = StringUtils.split((String)this.selectedValues, ";");
		}
		println(super.getOpenTag("select"));
		for(Entity bo:dataSource){
			if( bo == null) continue;
			String selected = "";
			if(ids != null){
				for(String id: ids){
					if(id.equals(String.valueOf(bo.getId()))){
						selected = " selected ";
						break;
					}
				}
			} else if(StringUtils.isEmpty(this.selectedProperty)){
				if(bo.equals(selectedValues)){
					selected = " selected ";
				}
			} else {
				try{
					Object value = bo.getProperty(this.selectedProperty);
					if(value.equals(this.selectedValues)){
						selected = " selected ";
					}
				}catch(Exception e){
					log.warn(this, e);
				}
			}
			println("<option value='"+bo.getId()+"'"+selected+">");
			print(bo.getName());
			print("</option>");
		}
		println(super.getCloseTag("select"));
		this.release();
		return SKIP_BODY;
	}
	
	public void release(){
		this.dataSource = null;
		this.dataSourceName= null;
		this.selectedValues = null;
		this.boClassName = null;
		this.query = null;
		super.release();
	}
}
